﻿using MoneyProtocol;
using MoneyProtocol.dto;
using Server.biz;
using Server.tool;
using NetFrame;
using NetFrame.auto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Server.logic.login
{
    public class LoginHandler : AbsOnceHandler, HandlerInterface
    {
        IAccountBiz accountBiz = BizFactory.accountBiz;

        public void ClientClose(NetFrame.UserToken token, string error)
        {
            ExecutorPool.Instance.execute(
                     delegate ()
                     {
                         accountBiz.close(token);
                     }
                     );
        }

        public void MessageReceive(NetFrame.UserToken token, NetFrame.auto.SocketModel message)
        {
            switch (message.command)
            {
                case LoginProtocol.LOGIN_CREQ:
                    login(token, message.GetMessage<AccountInfoDTO>());
                    break;
                case LoginProtocol.REG_CREQ:
                    reg(token, message.GetMessage<AccountInfoDTO>());
                    break;
                case LoginProtocol.UPDATEMSG_CREQ:
                    updateMsg(token, message.GetMessage<AccountInfoDTO>());
                    break;
                case LoginProtocol.GETMSG_CREQ:
                    getMsg(token);
                    break;
                case LoginProtocol.UPDATEPASSWORD_CREQ:
                    updatePassword(token, message.GetMessage<string>());
                    break;
                case LoginProtocol.OFFLINE_CREQ:
                    Console.WriteLine("用户离线");
                    accountBiz.close(token);
                    break;
            }
        }

        public void login(UserToken token, AccountInfoDTO value)
        {
            ExecutorPool.Instance.execute(
                delegate ()
                {
                    Console.WriteLine(value.account + "进行登录操作");
                    int result = accountBiz.login(token, value.account, value.password);
                    write(token, LoginProtocol.LOGIN_SRES, result);
                }
                );
        }
        public void reg(UserToken token, AccountInfoDTO value)
        {
            ExecutorPool.Instance.execute(
                    delegate ()
                    {
                        int result = accountBiz.create(token, value);
                        write(token, LoginProtocol.REG_SRES, result);
                    }
                    );
        }

        public void updateMsg(UserToken token, AccountInfoDTO value)
        {
            ExecutorPool.Instance.execute(
                    delegate ()
                    {
                        Console.WriteLine(value.account + "进行修改用户信息操作");
                        bool result = accountBiz.updateUserMsg(token, value);
                        write(token, LoginProtocol.UPDATEMSG_SRES, result);
                    }
                    );
        }

        public void updatePassword(UserToken token, string passwords)
        {
            ExecutorPool.Instance.execute(
                    delegate ()
                    {
                        Console.WriteLine("用户进行修改密码操作");
                        int result = accountBiz.updatePassword(token, passwords);
                        write(token, LoginProtocol.UPDATEPASSWORD_SRES, result);
                    }
                    );
        }

        public void getMsg(UserToken token)
        {
            ExecutorPool.Instance.execute(
                    delegate ()
                    {
                        Console.WriteLine("用户正在获取用户信息");
                        write(token, LoginProtocol.GETMSG_SRES, accountBiz.getUserMsg(token));
                    }
                    );
        }

        public void ClientConnect(NetFrame.UserToken token)
        {

        }

        public override byte GetType()
        {
            return Protocol.TYPE_LOGIN;
        }
    }
}
